package com.cskaoyan.service;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.domain.Storage;
import com.cskaoyan.bean.domain.StorageExample;
import com.cskaoyan.bean.storageBeans.StorageVo;
import com.cskaoyan.mapper.StorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @User: Yr
 * @Date: 2021/11/29
 * @Description:
 */

@Service
public class StorageServiceImpl implements StorageService {

    @Value("${upload.path}")
    String uploadPath;

    String BASEURL = "http://localhost:8083/wx/storage/fetch/";

    @Autowired
    StorageMapper storageMapper;

    @Override
    public StorageVo create(MultipartFile file) throws IOException {
        StorageVo storageVo = new StorageVo();
        String originalFilename = file.getOriginalFilename();
        storageVo.setName(originalFilename);
        String uuid = UUID.randomUUID().toString();
        storageVo.setKey(uuid + "-" + originalFilename);
        storageVo.setType(file.getContentType());
        storageVo.setSize((int) file.getSize());
        storageVo.setUrl( BASEURL + storageVo.getKey());
        storageVo.setAddTime(new Date());
        storageVo.setUpdateTime(new Date());
        storageMapper.insertStorageVo(storageVo);
        File file1 = new File(uploadPath, storageVo.getKey());
        file.transferTo(file1);

        return storageVo;
    }

    @Override
    public BaseData list(String name, String key, BaseParam baseParam) {

        //开启分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());

        //获取指定数据
        StorageExample storageExample = new StorageExample();
        if (name == null) {
            name = "";
        }
        if (key == null) {
            key = "";
        }
        storageExample.createCriteria().andNameLike("%" + name + "%").andKeyLike("%" + key + "%");
        storageExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<Storage> storages = storageMapper.selectByExample(storageExample);

        //获取分页信息
        PageInfo pageInfo = new PageInfo(storages);
        BaseData baseData = BaseData.list(storages, pageInfo.getTotal());
        baseData.setPage(pageInfo.getPageNum());
        baseData.setPages(pageInfo.getPages());
        baseData.setLimit(baseParam.getLimit());

        return baseData;
    }

    @Override
    public Storage update(Storage storage) {
        storage.setUpdateTime(new Date());
        storageMapper.updateByPrimaryKey(storage);
        Storage newSto = storageMapper.selectByPrimaryKey(storage.getId());
        return newSto;
    }

    @Override
    public int delete(Storage storage) {
        int affectedRows = storageMapper.deleteByPrimaryKey(storage.getId());
        return affectedRows;
    }
}
